﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using eTruck.Helpers;

namespace eTruck.Views.Authorizations
{
    public partial class RoleModules : System.Web.UI.Page
    {
        BLL.Authorization mrBLL = new BLL.Authorization();
        BLL.Role roleBLL = new BLL.Role();
        BLL.Modules mouduleBLL = new BLL.Modules();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGrid("");
                bindGridModule();
            }
        }

        private void userRowClick(int rowIndex)
        {
            //..
            int RoleID = 0;
            if (int.TryParse(grid_Role.DataKeys[rowIndex]["RoleID"].ToString(), out RoleID))
            {
                lab_CurrAccount.Text = grid_Role.Rows[rowIndex].Cells[0].Text.ToString();
                lab_Account.Text = RoleID.ToString();
                checkSelect(RoleID);
            }

        }

        private void checkSelect(int RoleID)
        {
            if (RoleID > 0)
            {

                DataTable dtUR = new DataTable();
                dtUR = mrBLL.GetModuleIDByRoleID(RoleID).Tables[0];


                for (int j = 0; j < grid_Module.Rows.Count; j++)
                {
                    CheckBox cbx = (CheckBox)grid_Module.Rows[j].FindControl("CheckBox1");
                    cbx.Checked = false;
                    if (dtUR.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtUR.Rows.Count; i++)
                        {
                            if (dtUR.Rows[i]["ModuleID"].ToString().Equals(grid_Module.DataKeys[j]["ModuleID"].ToString()))
                            {
                                cbx.Checked = true;
                            }
                        }
                    }
                }



            }
            btn_Search_Click(null, null);
        }

        protected void grid_Role_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover ", "c=this.style.backgroundColor;this.style.backgroundColor= '#00A9FF'");
                e.Row.Attributes.Add("onmouseout ", "this.style.backgroundColor=c ");
                e.Row.Attributes["style"] = "Cursor:pointer";

                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "SELECT$" + e.Row.RowIndex);

            }
        }

        //
        private void bindGridModule()
        {

            grid_Module.DataSource = mouduleBLL.GetAllList();
            grid_Module.DataBind();
        }

        private void bindGrid(string userName)
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();

            strWhere.AppendFormat("RouleName like '%{0}%'", userName);

            ds = roleBLL.GetList(strWhere.ToString());
            grid_Role.DataSource = ds;
            grid_Role.DataBind();
        }

        protected void grid_Role_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid_Role.PageIndex = e.NewPageIndex;

            string searchUser = txt_Search.Text;
            if (searchUser == null)
            {
                searchUser = "";
            }
            bindGrid(searchUser);
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            string searchUser = txt_Search.Text;
            if (searchUser == null)
            {
                searchUser = "";
            }
            bindGrid(searchUser);
        }
        protected void grid_Role_SelectedIndexChanged(object sender, EventArgs e)
        {
            int RoleID = 0;
            if (int.TryParse(grid_Role.DataKeys[grid_Role.SelectedRow.RowIndex]["RoleID"].ToString(), out RoleID))
            {
                lab_CurrAccount.Text = grid_Role.SelectedRow.Cells[0].Text.ToString();
                lab_Account.Text = RoleID.ToString();
                checkSelect(RoleID);
            }
        }

        //save userrole
        protected void btnSaveUR_Click(object sender, EventArgs e)
        {
            try
            {
                int RoleID = 0;

                if (int.TryParse(lab_Account.Text.Trim(), out RoleID))
                {
                    List<int> ModuleIDs = new List<int>();

                    for (int i = 0; i < grid_Module.Rows.Count; i++)
                    {
                        CheckBox cbx = (CheckBox)grid_Module.Rows[i].FindControl("CheckBox1");
                        if (cbx.Checked)
                        {
                            ModuleIDs.Add(Convert.ToInt32(grid_Module.DataKeys[i]["ModuleID"].ToString()));
                        }
                    }
                    if (mrBLL.UpdateRoleModule(RoleID, ModuleIDs))
                    {
                        MessageBoxHelper.MessageBox(Page, "保存成功！", "");
                    }
                    else
                    {
                        MessageBoxHelper.MessageBox(Page, "保存失败，请重试！", "");
                    }
                }
                else
                {
                    MessageBoxHelper.MessageBox(Page, "请选择用户", "");
                }

            }
            catch (Exception ex)
            {
                MessageBoxHelper.MessageBox(Page, "保存失败，请重试！" + ex.Message, "");
            }
            finally
            {
                bindGrid("");
            }
        }
    }
}